//
//  MPublicManager.m
//  DiDiHousekeeping
//
//  Created by Mask魏 on 14/11/25.
//  Copyright (c) 2014年 Mstarc. All rights reserved.
//

#import "MPublicManager.h"

@implementation MPublicManager
+ (instancetype)sharedPublicManager{
    static MPublicManager *sharedPublic=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedPublic=[[MPublicManager alloc]init];
    });
    return sharedPublic;
}

- (id)init{
    if (self=[super init]) {
    }
    return self;
}
+ (void)submitListName:(NSString *)listName obj:(NSObject *)obj successfulBlock:(void(^)(void))successfulBlock errorBlock:(void(^)(NSString *))errorBlock {
    BmobObject *gameScore = [BmobObject objectWithClassName:listName];
    NSDictionary *dic = [obj dictionaryRepresentation];
    for (NSString *key in dic.allKeys) {
        id value =dic[key];
        if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSMutableArray class]]) {
            [gameScore addObjectsFromArray:value forKey:key];//数组添加方式
        }else if ([key isEqualToString:@"author"]) {
            //数据关联不能用字典方式
             BmobUser *user= [BmobUser currentUser];
            [gameScore setObject:user forKey:key];
        }else {
            [gameScore setObject:value forKey:key];
        }
    }
    [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            successfulBlock();
        } else {
            errorBlock(@"失败");
        }
        //进行操作
        NSLog(@"添加成功===");
    }];
}

/**
 批量上传图片

 @param images <UIImage *>* 数组里只能是UIImage对象
 @param successfulBlock 成功返回 url 数组
 @param proportion 是否上传带有图片比例的 url
 @param errorBlock errorBlock
 */
+ (void)batchUploadImages:(NSArray <UIImage *>*)images proportion:(BOOL)proportion successfulBlock:(void(^)(NSArray* imageUrls))successfulBlock errorBlock:(void(^)(void))errorBlock {
    NSMutableArray * urls = [[NSMutableArray alloc] init];
   __block  int successfulCount=0;
  __block int errorCount = 0;
    for (UIImage *image in images) {
        CGFloat imageBili = image.size.height/image.size.width;//高除以宽得出比例
        NSData *data = UIImagePNGRepresentation(image);
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString * imageName = [NSString stringWithFormat:@"%@.png",str];
        BmobFile *file = [[BmobFile alloc]initWithFileName:imageName withFileData:data];
        [file saveInBackground:^(BOOL isSuccessful, NSError *error) {
            //如果文件保存成功，则把文件添加到filetype列
            if (isSuccessful) {
                  NSString * url = file.url;
                if (proportion) {
                    url = [NSString stringWithFormat:@"%f&%@",imageBili,file.url];
                }
                [urls addObject:url];
                NSLog(@"file.url %@",url);

                successfulCount++;
                if (successfulCount==images.count) {
                    if (successfulBlock) {
                        successfulBlock(urls);
                    }
                }
            }else{
                //进行处理
                errorCount++;
                if (errorCount==images.count) {
                    if (errorBlock) {
                        errorBlock();
                    }
                }
            }
        }];
    }
}

/**
 单张图片上传

 @param image UIImage 对象
 @param successfulBlock 成功后返回url
 @param errorBlock errorBlock
 */
+ (void)uploadImage:(UIImage *)image successfulBlock:(void(^)(NSString * urlStr))successfulBlock errorBlock:(void(^)(void))errorBlock {
    NSData *data = UIImagePNGRepresentation(image);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString * imageName = [NSString stringWithFormat:@"%@.png",str];
    BmobFile *file = [[BmobFile alloc]initWithFileName:imageName withFileData:data];
    [file saveInBackground:^(BOOL isSuccessful, NSError *error) {
        //如果文件保存成功，则把文件添加到filetype列
        if (isSuccessful) {
            NSLog(@"file.url %@",file.url);
          if (successfulBlock) {
                successfulBlock(file.url);
          }
        }else{
            if (errorBlock) {
                errorBlock();
            }
        }
    }];
}

#pragma mark 圆角阴影
- (void)RoundedCorners:(UIView *)roundedCorners Value:(int)value  {
    roundedCorners.layer.cornerRadius  = value;
    roundedCorners.layer.shadowOpacity = 0.5;
    roundedCorners.layer.shadowColor   = [UIColor grayColor].CGColor;
    roundedCorners.layer.shadowRadius  = 2;
    roundedCorners.layer.shadowOffset  = CGSizeMake(0, 2);
    roundedCorners.layer.masksToBounds = NO;
}
@end
